package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.Service.GoodService;
import com.shop.Service.LogService;
import com.shop.Utils.TokenUtils;
import com.shop.common.TokenService;
import com.shop.pojo.Brand;
import com.shop.pojo.Good;
import com.shop.pojo.Goods;
import com.shop.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/api/good")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private LogService logService;

    @PostMapping("/addGood")
    @ResponseBody
    public Message<String> addGood(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String sname =map.get("sname").toString();
        int number =  Integer.parseInt(map.get("number").toString());
        double price = Double.parseDouble(map.get("price").toString());
        int ppid = Integer.parseInt(map.get("ppid").toString());
        int ffid = Integer.parseInt(map.get("ffid").toString());
        String simgurl = map.get("simgurl").toString();
        Good good = new Good();
        good.setSname(sname);
        good.setNumber(number);
        good.setPrice(price);
        good.setPpid(ppid);
        good.setFfid(ffid);
        good.setSimgurl(simgurl);
        message = goodService.addGood(good);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,sname+"分类的添加");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,sname+"分类的添加");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/updateGood")
    @ResponseBody
    public Message<String> updateGood(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        int sid = Integer.parseInt(map.get("sid").toString());
        String sname =map.get("sname").toString();
        int number =  Integer.parseInt(map.get("number").toString());
        double price = Double.parseDouble(map.get("price").toString());
        int ppid = Integer.parseInt(map.get("ppid").toString());
        int ffid = Integer.parseInt(map.get("ffid").toString());
        String simgurl = map.get("simgurl").toString();
        Good good = new Good(sid,sname,number,price,ppid,ffid,simgurl);
        message = goodService.updateGood(good);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,sid+"分类的修改");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,sid+"分类的修改");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/deleteGood")
    @ResponseBody
    public Message<String> deleteGood(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        int sid = Integer.parseInt(map.get("sid").toString());
        message = goodService.deleteGood(sid);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,sid+"分类的删除");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,sid+"分类的删除");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/selectGood")
    @ResponseBody
    public Message<PageInfo<Goods>> selectGood(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception{
        Message<PageInfo<Goods>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = map.get("queryInfo").toString();
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = goodService.selectGood(queryInfo,pageSize,pageNum);
        message.setStatus("200");
        return message;
    }
    @RequestMapping(value = "/uploadGood",method = RequestMethod.POST)
    @ResponseBody
    public Message<String> upload(@RequestHeader(value = "Authorization",required = true) String Authorization,MultipartFile uploadFile) throws Exception {
            Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
            String originalFilename = uploadFile.getOriginalFilename();
            uploadFile.transferTo(new File("D:\\upload\\Good\\"+originalFilename));
            message.setStatus("201");
            message.setMessage("添加成功");
            message.setData(originalFilename);
            logService.addLog(tokenService.getUsername(Authorization),true,originalFilename+"商品图片的上传");
            return message;
    }
    @RequestMapping("/selectCount")
    @ResponseBody
    public Message<Integer> selectCount(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception{
        Message<Integer> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        message = goodService.selectCount();
        message.setStatus("200");
        return message;
    }
    @PostMapping("/selectById")
    @ResponseBody
    public Message<Good> selectById(@RequestBody Map<String,Object> map) throws Exception {
        Message<Good> message =new Message<>();
        int pid =  Integer.parseInt(map.get("sid").toString());
        message = goodService.selectById(pid);
        message.setStatus("200");
        return message;
    }

}
